<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Loader extends CI_Loader
{
    /**
     * List of paths to load event from
     *
     * @var array
     */
	protected $_ci_events_paths = array(APPPATH);

    /**
     * List of loaded events
     *
     * @var array
     */
    protected $_ci_events =    array();

    function __construct() {
        parent::__construct();
		$this->modules = array();
        log_message('debug', "MY_Loader Class Initialized");
    }
    
    
    /**
     * Event
     *
     * @param   string|string[] $events    Event name(s)
     * @return  object
     */
    public function event($events = array())
    {
        foreach ($this->_ci_prep_filename($events, '_event') as $event)
        {
          return  $this->model('events/'.$event);
        }
        // $this->_ci_events_class($event, $params, $object_name);
    }


    /**
     * Helper Event
     *
     * @param   string|string[] $events    Event name(s)
     * @return  object
     */
    public function event2($events = array())
    {

        foreach ($this->_ci_prep_filename($events, '_event') as $event)
        {
          return  $this->model('events/'.$event);
            if (isset($this->_ci_events[$event]))
            {
                continue;
            }

            // Is this a event extension request?
            $ext_helper = config_item('subclass_prefix').$event;
            $ext_loaded = FALSE;
            foreach ($this->_ci_events_paths as $path)
            {
                if (file_exists($path.'events/'.$ext_helper.'.php'))
                {
                    include_once($path.'events/'.$ext_helper.'.php');
                    $ext_loaded = TRUE;
                }
            }

            // If we have loaded extensions - check if the base one is here
            if ($ext_loaded === TRUE)
            {
                $base_helper = BASEPATH.'events/'.$event.'.php';
                if ( ! file_exists($base_helper))
                {
                    show_error('Unable to load the requested file: events/'.$event.'.php');
                }

                include_once($base_helper);
                $this->_ci_events[$event] = TRUE;
                log_message('info', 'Event loaded: '.$event);
                continue;
            }
            $CI =& get_instance();
            // No extensions found ... try loading regular events and/or overrides
            foreach ($this->_ci_events_paths as $path)
            {
                if (file_exists($path.'events/'.$event.'.php'))
                {
                    include_once($path.'events/'.$event.'.php');
                    $CI->$event = new $event();
                    $this->_ci_events[$event] = TRUE;
                    log_message('info', 'Event loaded: '.$event);
                    break;
                }
            }

            // unable to load the event
            if ( ! isset($this->_ci_events[$event]))
            {
                show_error('Unable to load the requested file: events/'.$event.'.php');
            }
        }

        return $this;
    }

	function module($module = '', $config = array(), $module_new_name = FALSE)
	{
		$method = 'run';
		
		if(($pos = strrpos($module, '/')) != FALSE) {
			$method = substr($module, $pos + 1);		
			$module = substr($module, 0, $pos);
		}
		
		$module_name = $module.'_module';
		if($module_new_name)
		{
			$module = $module_new_name;
		}
		
		if(!class_exists($module_name))
		{
			$this->model('modules/'.$module_name.'', $module);
			$this->modules[] = $module_name;
			log_message('debug', 'Module loaded: '.$module_name);
		}
	}
}


?>